#smooth the data 
smoothData <- function(timeSeries, MeanDepth, IndexTimeSeries) {
  ret <- as.vector(rollapply(timeSeries, MeanDepth, mean, align="right"))
  return(as.xts(ret,order.by=IndexTimeSeries[MeanDepth:length(IndexTimeSeries)]))
}

smoothData(zf,4,IndexZf)

# AR on smoothed data
ARSmoothedData <- function(maxAROrder, ARtype, MeanDepth) {
  return(
    function(timeSeries){
      smoothedTimeSeries = smoothData(timeSeries,MeanDepth,index(timeSeries))
      options(warn=-1)
      sim <- ar(x=smoothedTimeSeries, order.max=maxAROrder, method=ARtype, demean=FALSE)
      options(warn=1)
      gc()
      return((predict(sim, n.ahead=1)$pred)[1])
    })
}

ARSmoothedDataPredict <-ARSmoothedData(1,'ols',1)

ret <-ownPredict(zf,ARSmoothedDataPredict,10000,function(x) return(x), simDeTrans)
ret <-ownPredict(cl,ARSmoothedDataPredict,10000,function(x) return(x), simDeTrans)
ret <-ownPredict(google,ARSmoothedDataPredict,250,logReturnTransform, logReturnDeTransform)

plot(ret$x2)
lines(ret$y2)
ret$aafe

output = c(1,2); 
c(output, 6)
output2 = c(3,4,5)
rbind(output, output2)

meanDepthVec =c();
aafeVec = c();
for (meanDepth in 0:10){
  meanDepthVec <- cbind(meanDepthVec,meanDepth)
  ARSmoothedDataPredict <-ARSmoothedData(1,'ols',meanDepth)
  ret <-ownPredict(google,ARSmoothedDataPredict,250,diffTransform, diffDeTransform)
  aafeVec <- cbind(aafeVec,ret$aafe)  
}
out = rbind(meanDepthVec, aafeVec); out

min(abs(diff(zf)[-1]))
